package selected;

import java.util.ArrayList;
import java.util.List;

public class J0829 {
    // 1.子集
    List<List<Integer>> ret;
    List<Integer> path;

    public List<List<Integer>> subsets(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        dfs(nums, 0);
        return ret;
    }

    public void dfs(int[] nums, int i){
        if(i == nums.length) {
            ret.add(new ArrayList<>(path));
            return;
        }
        // 不选择当前值
        dfs(nums, i + 1);
        // 选择当前值
        path.add(nums[i]);
        dfs(nums, i + 1);
        path.remove(path.size() - 1);
    }
    // 2.电话号码
    List<String> ret1;
    StringBuffer path1;
    String[] s = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        ret1 = new ArrayList<>();
        path1 = new StringBuffer();
        if(digits.isEmpty()) return ret1;
        dfs(digits, 0);
        return ret1;
    }

    public void dfs(String digits, int pos){
        if(path1.length() == digits.length()){
            ret1.add(path1.toString());
            return;
        }
        String cur = s[digits.charAt(pos) - '0'];
        for(int i = 0; i < cur.length(); i++){
            path1.append(cur.charAt(i));
            dfs(digits, pos + 1);
            // 回溯
            path1.deleteCharAt(path1.length() - 1);
        }
    }
}
